<template>
  <div class="app-container">
    <!-- 页面头部查询   -->
    <!--
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="酒店ID" prop="hotelId">
            <el-input
              v-model="queryParams.hotelId"
              placeholder="请输入酒店ID"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="每晚价格" prop="pricePerNight">
            <el-input
              v-model="queryParams.pricePerNight"
              placeholder="请输入每晚价格"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="客房总数" prop="totalRooms">
            <el-input
              v-model="queryParams.totalRooms"
              placeholder="请输入客房总数"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="剩余客房数量" prop="remainingRooms">
            <el-input
              v-model="queryParams.remainingRooms"
              placeholder="请输入剩余客房数量"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="creationDate">
            <el-date-picker clearable
              v-model="queryParams.creationDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择创建时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="创建者ID" prop="createdBy">
            <el-input
              v-model="queryParams.createdBy"
              placeholder="请输入创建者ID"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:rooms:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:rooms:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:rooms:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:rooms:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="客房类型" align="center" prop="roomType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hbms_rooms_status" :value="scope.row.roomType"/>
        </template>
      </el-table-column>
      <el-table-column label="每晚价格" align="center" prop="pricePerNight"/>
      <el-table-column label="客房总数" align="center" prop="totalRooms"/>
      <el-table-column label="剩余客房数量" align="center" prop="remainingRooms"/>
      <el-table-column label="客房描述" align="center" prop="description"/>
      <el-table-column label="创建时间" align="center" prop="creationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者ID" align="center" prop="createdBy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:rooms:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:rooms:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改客房信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="酒店ID" prop="hotelId">
                  <el-input v-model="form.hotelId" placeholder="请输入酒店ID"/>
                </el-form-item>-->
        <el-form-item label="客房类别">
          <el-select v-model="form.roomType" placeholder="请选择客房类别">
            <el-option
              v-for="dict in dict.type.hbms_rooms_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="每晚价格" prop="pricePerNight">
          <el-input v-model="form.pricePerNight" placeholder="请输入每晚价格"/>
        </el-form-item>
        <el-form-item label="客房总数" prop="totalRooms">
          <el-input v-model="form.totalRooms" placeholder="请输入客房总数"/>
        </el-form-item>
        <el-form-item label="剩余客房" prop="remainingRooms">
          <el-input v-model="form.remainingRooms" placeholder="请输入剩余客房数量"/>
        </el-form-item>
        <!--        <el-form-item label="创建时间" prop="creationDate">
                  <el-date-picker clearable
                                  v-model="form.creationDate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="请选择创建时间">
                  </el-date-picker>
                </el-form-item>-->
        <!--        <el-form-item label="创建者ID" prop="createdBy">
                  <el-input v-model="form.createdBy" placeholder="请输入创建者ID"/>
                </el-form-item>-->
        <el-form-item label="客房描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRooms, getRooms, delRooms, addRooms, updateRooms} from "@/api/business/rooms";

export default {
  name: "Rooms",
  dicts: ['hbms_rooms_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客房信息管理表格数据
      roomsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hotelId: null,
        roomType: null,
        description: null,
        pricePerNight: null,
        totalRooms: null,
        remainingRooms: null,
        creationDate: null,
        createdBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hotelId: [
          {required: true, message: "酒店ID不能为空", trigger: "blur"}
        ],
        roomType: [
          {required: true, message: "客房类型不能为空", trigger: "change"}
        ],
        pricePerNight: [
          {required: true, message: "每晚价格不能为空", trigger: "blur"}
        ],
        totalRooms: [
          {required: true, message: "客房总数不能为空", trigger: "blur"}
        ],
        remainingRooms: [
          {required: true, message: "剩余客房数量不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客房信息管理列表 */
    getList() {
      this.loading = true;
      listRooms(this.queryParams).then(response => {
        this.roomsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        hotelId: null,
        roomType: null,
        description: null,
        pricePerNight: null,
        totalRooms: null,
        remainingRooms: null,
        creationDate: null,
        createdBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客房信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roomId = row.roomId || this.ids
      getRooms(roomId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客房信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roomId != null) {
            updateRooms(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRooms(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roomIds = row.roomId || this.ids;
      this.$modal.confirm('是否确认删除客房信息管理编号为"' + roomIds + '"的数据项？').then(function () {
        return delRooms(roomIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/rooms/export', {
        ...this.queryParams
      }, `rooms_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
